#!/bin/bash
. /etc/hifiberry.state

function check_systemd {
 echo -n "$1: "
 systemctl is-active $2 1>/dev/null
 if [ $? == 0 ]; then
   echo running
 else
   echo not running
 fi
}

echo -n "HiFiBerryOS version: "
cat /etc/hifiberry.version

echo "Pi type: $CURRENT_PIMODEL"
echo -n "Pi model: $MODEL"
cat /proc/device-tree/model
echo ""

echo -n "CPU temperature: "
awk '{c=$1/1000; printf "%.1f°C %.1f°F\n", c, c*1.8+32}' /sys/class/thermal/thermal_zone0/temp
echo ""

echo -n "Compatibility: "
/opt/hifiberry/bin/check-compatibility
APLAY=`aplay -l | grep card | head -1`
C1=`echo $APLAY | awk -F, '{print $1}' | sed s/://g`
C2=`echo $APLAY | awk -F, '{print $2}' | sed s/://g`

echo "Sound card: $CURRENT_CARD"
echo "Sound card ID: $CURRENT_HW_SHORT"
echo "aplay 1: $C1"
echo "aplay 2: $C2"

echo "Mixer: $CURRENT_MIXER_CONTROL"
echo "Features: $CURRENT_FEATURES"

echo -n "Power controller: "
LSB=`i2cget -y 1 0x77 0xfd`
MSB=`i2cget -y 1 0x77 0xfe`
PCVERSION="$MSB/$LSB"
if [ "$PCVERSION" == "/" ]; then
 PCVERSION="not detected"
fi
echo $PCVERSION

echo -n "Linux version: "
uname -a

echo -n "Date and time: "
date

ETH0=`ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'`
if [ "$ETH0" != "" ]; then
 echo "eth0: $ETH0"
fi

WLAN0=`ifconfig wlan0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'`
if [ "$WLAN0" != "" ]; then
 echo "eth0: $WLAN0"
fi

echo -n "/data mounted: "
M=`mount | grep " /data "`
if [ "$M" == "" ]; then
  echo FAIL
else
  echo OK
fi 

echo -n "/dev/dri/card0: "
if [ -c /dev/dri/card0 ]; then 
  echo OK
else
  echo NO
fi

echo -n "BT devices: "
DEV=""
for INT in `hciconfig | grep hci | awk -F: '{print $1}'`; do
 MANUFACTURER=`hciconfig -a $INT | grep "Manufacturer:" | awk -F: '{print $2}'`
 DEV="$DEV $INT - $MANUFACTURER,"
done 
echo $DEV | sed s/,\$//

echo -n "WiFi devices: "
DEV=""
for INT in `iwconfig 2>/dev/null | grep wlan | awk '{print $1}'`; do 
 DEV="$DEV $INT,"
done
echo $DEV | sed s/,\$//

# Daemons
for i in audiocontrol2 beocreate2 bluetooth bluealsa bluealsa-aplay dlnampris mpd pigpio raat shairport-sync sigmatcp snapcastmpris spotify squeezelite sshd ympd weston cog; do
 check_systemd $i $i.service
done
